package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.OrganizationBase;

import com.atguigu.eduservice.mapper.OrganizationBaseMapper;
import com.atguigu.eduservice.service.OrganizationBaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class OrganizationBaseServiceImpl extends ServiceImpl<OrganizationBaseMapper, OrganizationBase> implements OrganizationBaseService {


    @Override
    public List<OrganizationBase> listWithTree() {
        List<OrganizationBase> organizationBases = baseMapper.selectList(null);
        return  organizationBases.stream()
                //查找到所有的一级菜单
                .filter(org -> org.getParentCode().equals("0"))
                .map(org ->{
                 org.setChildren(getChildren(org,organizationBases));
                 return org;
                }).collect(Collectors.toList());

    }
    /**
     * 递归获取子菜单
     *
     */
    public List<OrganizationBase> getChildren(OrganizationBase o,List<OrganizationBase> all){
        List<OrganizationBase> children =  all.stream()
                .filter(org -> org.getParentCode().equals(o.getOrgCode()) )
                .map((org) ->{
                    org.setChildren(getChildren(org,all));
                    return org;
                }).collect(Collectors.toList());
        return children;
    }
}
